package cronjob

import (
	"context"
	"fmt"
	"peilian-api/app/global/common"
	"peilian-api/app/global/log"
	"peilian-api/app/services/agent_service"
	"peilian-api/app/services/career_service"
	"peilian-api/app/services/conf_service"
	"peilian-api/app/services/label_service"
	"peilian-api/app/services/online_service"
	"peilian-api/app/services/report_service"
	"peilian-api/app/services/resume_service"
	"peilian-api/app/services/tiku_service"
	"peilian-api/app/services/user_service"
	"peilian-api/utils/tools"
	"strings"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"github.com/robfig/cron/v3"
)

// c 定时器
var c *cron.Cron

func InitCron() {
	c = cron.New(cron.WithLocation(time.UTC)) // 定时任务
	c.AddFunc("10 01 * * *", func() {
		agent_service.CalculationServiceTime()
	})

	// 构建侧边栏
	c.AddFunc("*/1 */23 * * *", func() {
		new(conf_service.ConstituteService).SetRedisConsituteSadminKey(false)
	})

	// 实时构建
	c.AddFunc("*/1 * * * *", func() {
		new(conf_service.ConstituteService).SetRedisConsituteSadminKey(true)
	})

	// 检测5分钟内有没有报告生成
	c.AddFunc("*/2 * * * *", func() {
		// 设置TraceKey，不然会panic
		c := &gin.Context{}
		uuidStr := strings.ReplaceAll(uuid.NewString(), "-", "")
		c.Set(common.TraceKey, context.WithValue(context.Background(), common.TraceKey, &log.Trace{TraceId: uuidStr, SpanId: uuidStr, Caller: "/judgeReport"}))
		new(report_service.SessionService).JudgeReport(c)
	})

	// 每天凌晨刷新一次 专项练习tab标签
	c.AddFunc("0 0 * * *", func() {
		new(career_service.SpecialService).SetSpecialCareerList()
	})

	// 每天凌晨刷新一次 刷新会员状态
	c.AddFunc("1 0 * * *", func() {
		user_service.MemberStatus()
	})

	// 自定义标签生成
	c.AddFunc("*/2 * * * *", func() {
		label_service.AsyncGetTask()
	})

	//// 删除不使用的记录
	//c.AddFunc("* * * * *", func() {
	//	middleware.DelSetData()
	//})

	// 同步redis与mysql数据 主要校对两个字段
	//1.peilian_resume_is_viewed_user_XXX
	//2.peilian_resume_is_generate_user_XXX
	c.AddFunc("0 */12 * * *", func() {
		// 设置TraceKey，不然会panic
		c := &gin.Context{}
		uuidStr := strings.ReplaceAll(uuid.NewString(), "-", "")
		c.Set(common.TraceKey, context.WithValue(context.Background(), common.TraceKey, &log.Trace{TraceId: uuidStr, SpanId: uuidStr, Caller: "/judgeResume"}))
		new(resume_service.ResumeService).JudgeResume(c)
	})

	c.Start()
}

func loopRun() {
	defer func() {
		fmt.Println("GenerationResumeFinish exit")
		if rErr := recover(); rErr != nil {
			fmt.Println(rErr)
		}
		tools.RecoverGoroutine()
		loopRun()
	}()
	c := &gin.Context{}
	uuidStr := strings.ReplaceAll(uuid.NewString(), "-", "")
	c.Set(common.TraceKey, context.WithValue(context.Background(), common.TraceKey, &log.Trace{TraceId: uuidStr, SpanId: uuidStr, Caller: "/resumfinish"}))
	for {
		new(resume_service.ResumeService).GenerationResumeFinish(c)
		time.Sleep(30 * time.Second)
	}
}

func PermanentTask() {
	// 设置TraceKey，不然会panic
	c := &gin.Context{}
	uuidStr := strings.ReplaceAll(uuid.NewString(), "-", "")
	c.Set(common.TraceKey, context.WithValue(context.Background(), common.TraceKey, &log.Trace{TraceId: uuidStr, SpanId: uuidStr, Caller: ""}))

	go func() {
		defer tools.RecoverGoroutine()
		new(career_service.SpecialService).JudgeSpecialReport(c)
	}()

	// 队列刷新报告
	go func() {
		defer tools.RecoverGoroutine()
		new(career_service.SpecialService).DealSession(c)
	}()

	go func() {
		defer tools.RecoverGoroutine()
		online_service.JudgeNoPcmNoMp3(c)
	}()
	// 轮询glm生成结果
	go loopRun()

	// 轮询视频生成
	go new(tiku_service.TikuService).LoopGenerationVideoFinish(c)

	// 构建ES
	go func() {
		defer tools.RecoverGoroutine()
		for {
			SessionDataSyncWithElasticsearch(0)
			<-time.After(10 * time.Second)
		}
	}()

}
